﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculatingSums
{
    public class SumDelegate
    {
        public delegate decimal CalculatingSumsDelegate(int n);
        
        private static decimal FractionsSum(int denumerator) 
        {
            return (decimal) 1/denumerator;
        }

        private static decimal FractionFactorialSum(int n) 
        {
            decimal result = 1;
            decimal factorial = 1;
            for (int i = 2; i < n; i++) 
            {
                factorial*=i;
                result += (decimal)1 / factorial;
            }
            return result;
        }

        private static decimal FractionsSumAndDifference() 
        {
            decimal sum = 1;
            int counter = 1;
            for (int i = 2; i < 10001; i+=2) 
            {
                if (counter % 2 == 0)
                {
                    sum -= (decimal)1 / i;
                }
                else 
                {
                    sum += (decimal)1 / i;
                }
                counter++;
            }
            return sum;
        }

        public static decimal CalculateFractionSum() 
        {
            CalculatingSumsDelegate calculator = new CalculatingSumsDelegate(FractionsSum);
            decimal sum = 1;
            for (int i = 2; i < 10000000; i*=2) 
            {
                sum += calculator(i);
            }
            return sum;
        }

        public static decimal CalculateFractionsFactorialSum(int n) 
        {
            CalculatingSumsDelegate calculator = new CalculatingSumsDelegate(FractionFactorialSum);
            return calculator(n);
        }
    }
}
